﻿using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using élénkPróbája.extentions;
using élénkPróbája.Test.dto;
using élénkPróbája.Test.extention;
using élénkPróbája.validators;

namespace élénkPróbája.Test
{
    [TestClass]
    public class ExactLengthValidatorTester
    {
        public ExactLengthValidatorTester()
        {
            CultureScope.SetDefaultCulture();
        }

        [TestMethod]
        public void When_the_text_is_an_exact_length_the_validator_should_pass()
        {
            var validator = new TestValidator { v => v.RuleFor(x => x.PublicName).Length(4) };
            var result = validator.Validate(new Company() { PublicName = "test" });
            result.IsValid.ShouldBeTrue();
        }
        [TestMethod]
        public void When_the_text_length_is_smaller_the_validator_should_fail()
        {
            var validator = new TestValidator { v => v.RuleFor(x => x.PublicName).Length(10) };
            var result = validator.Validate(new Company() { PublicName = "test" });
            result.IsValid.ShouldBeFalse();
        }
        [TestMethod]
        public void When_the_text_length_is_larger_the_validator_should_fail()
        {
            var validator = new TestValidator { v => v.RuleFor(x => x.PublicName).Length(1) };
            var result = validator.Validate(new Company() { PublicName = "test" });
            result.IsValid.ShouldBeFalse();
        }
        [TestMethod]
        public void When_the_validator_fails_the_error_message_should_be_set()
        {
            var validator = new TestValidator { v => v.RuleFor(x => x.PublicName).Length(2) };
            var result = validator.Validate(new Company() { PublicName = "test" });
            result.Errors.Single().ErrorMessage.ShouldEqual("'' must be 2 characters in length. You entered 4 characters.");
        }
        [TestMethod]
        public void Min_and_max_properties_should_be_set()
        {
            var validator = new ExactLengthValidator(5);
            validator.Min.ShouldEqual(5);
            validator.Max.ShouldEqual(5);
        }
        [TestMethod]
        public void When_exact_length_rule_failes_error_should_have_exact_length_error_errorcode()
        {
            var validator = new TestValidator { v => v.RuleFor(x => x.PublicName).Length(2) };

            var result = validator.Validate(new Company() { PublicName = "test" });
            var error = result.Errors.SingleOrDefault(e => e.ErrorCode == "exact_length_error");

            error.ShouldNotBeNull();
            error.PropertyName.ShouldEqual("PublicName");
            error.AttemptedValue.ShouldEqual("test");
            error.FormattedMessageArguments.Length.ShouldEqual(0);

            error.FormattedMessagePlaceholderValues.Count.ShouldEqual(5);
            error.FormattedMessagePlaceholderValues.ContainsKey("PropertyName").ShouldBeTrue();
            error.FormattedMessagePlaceholderValues.ContainsKey("PropertyValue").ShouldBeTrue();
            error.FormattedMessagePlaceholderValues.ContainsKey("MinLength").ShouldBeTrue();
            error.FormattedMessagePlaceholderValues.ContainsKey("MaxLength").ShouldBeTrue();
            error.FormattedMessagePlaceholderValues.ContainsKey("TotalLength").ShouldBeTrue();

            //error.FormattedMessagePlaceholderValues["PropertyName"].ShouldEqual("PublicName");
            error.FormattedMessagePlaceholderValues["PropertyValue"].ShouldEqual("test");
            error.FormattedMessagePlaceholderValues["MinLength"].ShouldEqual(2);
            error.FormattedMessagePlaceholderValues["MaxLength"].ShouldEqual(2);
            error.FormattedMessagePlaceholderValues["TotalLength"].ShouldEqual(4);
        }
    }
}
